<?php

	include_once '../com/mss/model/User.php';
	include_once '../com/mss/accessor/UserAccessor.php';

	class UserService {
	
		public function addUser(User $user) {
			$errors = $this->validate($user);
			$results = array();
			if (count($errors, 1) == 0) {
				$user_accessor = new UserAccessor();
				$user_accessor->save($user);
				$results['success'] = "YES";
				$results['message'] = "User added successfully!!!";	
			} else {
				$results['success'] = "NO";
				$results['errors'] = $errors;
			}
			return $results;
		}
		
		public function update(User $user) {
			$errors = array();
			$results = array();
			if (count($errors, 1) == 0) {
				$user_accessor = new UserAccessor();
				$user_accessor->update($user);
				$results['success'] = "YES";
				$results['message'] = "User updated successfully!!!";	
			} else {
				$results['success'] = "NO";
				$results['errors'] = $errors;
			}
			return $results;			
		}
		
		public function selectAll() {
			$user_accessor = new UserAccessor();
			return $user_accessor->selectAll();
		}
		
		public function view($id) {
			$user_accessor = new UserAccessor();
			return $user_accessor->select($id);
		}
		
		private function validate(User $user) {
			$errors = array();
			$first_name = $user->getFirstName();
			if (empty($first_name)) {
				$errors['first_name'] = "You need to enter your first name";
			}
			$login_id = $user->getLoginID();
			if (empty($login_id)) {
				$errors['login_id'] = "You need to have a login ID";
			}
			$password = $user->getPassword();
			if (empty($password)) {
				$errors['password'] = "You need a to enter a password";
			}
			return $errors;
		}
	}

?>